package com.pointi.test.twittclonetest.util.rest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;

import com.pointi.test.twittclonetest.TwittCloneTestActivity;

public class RestHelper {
	private static String LOG_TAG = TwittCloneTestActivity.LOG_TAG; 
	public String execPost(String url , ArrayList<NameValuePair> params){
		try {
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost(url);
			
			UrlEncodedFormEntity ent = null;
			
			String authHeader = "";
			authHeader += "OAuth ";
			int nCount = params.size();
			ArrayList<NameValuePair> alCallbackBody = new ArrayList<NameValuePair>();
			
			
			for(NameValuePair pair: params){
				nCount--;
				authHeader += pair.getName() + "=\"" + pair.getValue() +"\"";
				if("oauth_callback".equalsIgnoreCase(pair.getName())){
					NameValuePair tmpPair = new BasicNameValuePair(pair.getName(), TwittCloneTestActivity.REG_CALLBACK);
					alCallbackBody.add(tmpPair);
				}
				if(nCount >0){
					authHeader+=", ";
				}
			}
			
			Log.d(LOG_TAG, "["+authHeader+"]");
			
			post.setHeader("Authorization", authHeader);
			
			Header[] tmpGet = post.getHeaders("Authorization");
			Log.d(LOG_TAG, "["+tmpGet[0].getValue()+"]");
			
			
			if(alCallbackBody != null)
				ent = new UrlEncodedFormEntity(alCallbackBody, HTTP.UTF_8);
			
			
//			post.setEntity(ent);
			HttpResponse resPost = client.execute(post);
			HttpEntity resEntity = resPost.getEntity();
			
			int nStatusCode = resPost.getStatusLine().getStatusCode();
			
//			if(nStatusCode != 200){
//				Log.d(LOG_TAG, "HTTP Response Code["+nStatusCode+"]");
//				return null;
//			}
			
			Log.d(LOG_TAG, "HTTP Response Code["+nStatusCode+"]");
			
			InputStream is = resEntity.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
			
			StringBuilder sb = new StringBuilder();
			
			String line = null;
			
		    while ((line = reader.readLine()) != null) {
		    	sb.append(line);
		    }
		    is.close();
		    
		    Log.d(LOG_TAG, "HTTP Response Body["+sb.toString()+"]");

		    return sb.toString();
			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	public String execGet(String url , ArrayList<NameValuePair> params){
		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet get = new HttpGet(url);
			
			UrlEncodedFormEntity ent = null;
			
			String authHeader = "";
			authHeader += "OAuth ";
			int nCount = params.size();
			ArrayList<NameValuePair> alCallbackBody = new ArrayList<NameValuePair>();
			
			
			for(NameValuePair pair: params){
				nCount--;
				authHeader += pair.getName() + "=\"" + pair.getValue() +"\"";
				if("oauth_callback".equalsIgnoreCase(pair.getName())){
					NameValuePair tmpPair = new BasicNameValuePair(pair.getName(), TwittCloneTestActivity.REG_CALLBACK);
					alCallbackBody.add(tmpPair);
				}
				if(nCount >0){
					authHeader+=", ";
				}
			}
			
			Log.d(LOG_TAG, "["+authHeader+"]");
			
			get.setHeader("Authorization", authHeader);
			
			Header[] tmpGet = get.getHeaders("Authorization");
			Log.d(LOG_TAG, "["+tmpGet[0].getValue()+"]");
			
			
			if(alCallbackBody != null)
				ent = new UrlEncodedFormEntity(alCallbackBody, HTTP.UTF_8);
			
			
//			post.setEntity(ent);
			HttpResponse resGet = client.execute(get);
			HttpEntity resEntity = resGet.getEntity();
			
			int nStatusCode = resGet.getStatusLine().getStatusCode();
			
//			if(nStatusCode != 200){
//				Log.d(LOG_TAG, "HTTP Response Code["+nStatusCode+"]");
//				return null;
//			}
			
			Log.d(LOG_TAG, "HTTP Response Code["+nStatusCode+"]");
			
			InputStream is = resEntity.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
			
			StringBuilder sb = new StringBuilder();
			
			String line = null;
			
		    while ((line = reader.readLine()) != null) {
		    	sb.append(line);
		    }
		    is.close();
		    
		    Log.d(LOG_TAG, "HTTP Response Body["+sb.toString()+"]");

		    return sb.toString();
			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
}
